package com.bistu.sim.xwy.news.UserMode;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.bistu.sim.xwy.news.Adapter.ArticleAdapter;
import com.bistu.sim.xwy.news.MainActivity;
import com.bistu.sim.xwy.news.Other.ArticlePageActivity;
import com.bistu.sim.xwy.news.R;
import com.bistu.sim.xwy.news.domain.Article;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MessageActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MessageActivity";
    private List<Article> list;
    private Article article;
    private ArticleAdapter adapter;
    private ListView listView;
    private LinearLayout index_bottom_bar_home;
    private LinearLayout index_bottom_bar_dynamic_state;
    private LinearLayout index_bottom_bar_me;
    private LinearLayout index_bottom_bar_integral;
    private ImageView index_botton_bar_scan;
    private LinearLayout index_bottom_bar_post;
    private ImageView index_bottom_bar_post_image;

    private LinearLayout index_bootom_saler;
    private ImageView index_bottom_bar_home_image;
    private ImageView index_bottom_bar_dynamic_state_image;
    private ImageView index_bottom_bar_integral_image;
    private ImageView index_bottom_bar_me_image;

    private ListView art_list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        initView();
        DoGet();
//        list = new ArrayList<Article>();
        //  Log.e("aaa",list.get(0).getTitle());
//        adapter = new ArticleAdapter(MessageActivity.this,list);
//        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                article = list.get(position);
                //Toast.makeText(MessageActivity.this,article.getTitle(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MessageActivity.this, ArticlePageActivity.class);
                intent.putExtra("title",article.getTitle());
                intent.putExtra("content",article.getContent());
                intent.putExtra("category",article.getCategories());
                startActivity(intent);
            }
        });



    }
    //初始化控件
    private void initView() {

        index_bottom_bar_home = findViewById(R.id.index_bottom_bar_home);
        index_bottom_bar_dynamic_state = findViewById(R.id.index_bottom_bar_dynamic_state);
        index_bottom_bar_me=findViewById(R.id.  index_bottom_bar_me);
        index_bottom_bar_integral=findViewById(R.id.index_bottom_bar_integral);
        index_botton_bar_scan=findViewById(R.id.index_bottom_bar_scan);
        index_bootom_saler=findViewById(R.id.index_bottom_post);
        index_bottom_bar_post_image = (ImageView) findViewById(R.id.index_bottom_post_img);
        index_bottom_bar_post = findViewById(R.id.index_bottom_post);

        index_bottom_bar_home_image = (ImageView) findViewById(R.id.index_bottom_bar_home_image);
        index_bottom_bar_dynamic_state_image = (ImageView) findViewById(R.id.index_bottom_bar_dynamic_state_image);
        index_bottom_bar_integral_image = (ImageView) findViewById(R.id.index_bottom_bar_integral_image);
        index_bottom_bar_me_image = (ImageView) findViewById(R.id.index_bottom_bar_me_image);
        listView = (ListView)findViewById(R.id.art_list);
//        list = new ArrayList<Article>();
        //从后端读出article数据
//        list.add(new Article("第一个","nan","散文",R.drawable.c));
//        list.add(new Article("第二个","nan","散文",R.drawable.b));
//        list.add(new Article("第三个","nan","散文",R.drawable.a));
//        list.add(new Article("第四个","nan","散文",R.drawable.d));
//        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .connectTimeout(8000, TimeUnit.MILLISECONDS)
//                .build();
//        Request request = new Request.Builder()
//                .get()
//                .url(getString(R.string.article_url))
//                .build();
//        okHttpClient.newCall(request).enqueue(new Callback() {
//            @Override
//            public void onFailure(@NonNull Call call, @NonNull IOException e) {
//                Log.e(TAG,"请求失败"+e.toString());
//
//            }
//
//            @Override
//            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//                Log.e(TAG,"请求成功"+response.code());
//                Log.e(TAG,"请求数据"+response.body().string());
//
//            }
//        });








        //数据读出
        index_bottom_bar_integral_image.setImageResource(R.mipmap.index_bottom_bar_integral_select);

        index_bottom_bar_home.setOnClickListener(this);
        index_bottom_bar_dynamic_state.setOnClickListener(this);
        index_bottom_bar_me.setOnClickListener(this);
        index_bottom_bar_integral.setOnClickListener(this);
        index_botton_bar_scan.setOnClickListener(this);
        index_bottom_bar_post.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.index_bottom_bar_home:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                MessageActivity.this.finish();
                overridePendingTransition(0,0);
                break;
            case  R.id.index_bottom_bar_integral:
                Intent intent1 = new Intent(this,MessageActivity.class);
                startActivity(intent1);
                MessageActivity.this.finish();
                overridePendingTransition(0,0);
                break;
            case  R.id.index_bottom_bar_dynamic_state:
                Intent intent2 = new Intent(this,FocusActivity.class);
                startActivity(intent2);
                MessageActivity.this.finish();
                overridePendingTransition(0,0);
                break;
            case  R.id.index_bottom_bar_me:
                Intent intent3 = new Intent(this,MineActivity.class);
                startActivity(intent3);
                MessageActivity.this.finish();
                overridePendingTransition(0,0);
                break;
            case R.id.index_bottom_bar_scan:
                Intent intent4 = new Intent(this,PublishActivity.class);
                startActivity(intent4);
                break;
            case R.id.index_bottom_post:
                Intent intent5 = new Intent(this, PostActivity.class);
                startActivity(intent5);
                MessageActivity.this.finish();
                overridePendingTransition(0,0);
                break;
        }
    }

    public void DoGet(){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(8000, TimeUnit.MILLISECONDS)
                .build();
        Request request = new Request.Builder()
                .get()
                .url(getString(R.string.article_url)+"info/article")
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e(TAG,"请求失败"+e.toString());

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String body=response.body().string();
                Log.e(TAG,"请求成功"+response.code());
                Log.e(TAG,"请求数据"+body);
                Gson gson = new Gson();
                list = gson.fromJson(body, new TypeToken<List<Article>>(){}.getType());
//                Log.e("aaaaaa",list.get(0).getTitle());
//                Log.e("aa",list.get(1).getContent());
//                Log.e("aaaaa", String.valueOf(list.size()));
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new ArticleAdapter(MessageActivity.this,list);
                        listView.setAdapter(adapter);
                    }
                });
            }
        });
    }
}
